<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class LinkseController extends commencontroller
{
    //

    ////GET.Admin/links
    function index(){

        $linkresoult=DB::table('links')
            ->orderBy('link_id','desc')
            ->paginate(5);

        return view('Admin.links.index',compact('linkresoult'));

    }
    public function linksorder(){
        $input=Input::all();
        $link_id=$input['link_id'];
       $linkorder= $input['linkorder'];
       $linkcounte=DB::table('links')
           ->where('link_id',$link_id)
           ->update(
               array('link_order'=>$linkorder)
        );

       if ($linkcounte>0){
           $date=[
               'status'=>1,
               'msg'=>'修改排序成功'
           ];
       }else{
           $date=[
               'status'=>0,
               'msg'=>'修改失败。请稍后再试'
           ];

       }


       return $date;
    }

//GET|HEAD.Admin/links/create添加
    function create (){
        return view('Admin/links/add');
    }

    //post. Admin/index
    function store (){
        $input=Input::except('_token','link_id');
        $rules=[
            'link_name'=>'required',


            'link_url'=>'required'

        ];
        $message=[
            'link_name.required'=>'链接名称不能为空',

            'link_url.required'=>'友情链接不能为空'
        ];
        $validator=Validator::make($input,$rules,$message);

        if($validator->passes()){
            $adresoult=DB::table('links')
                ->insert(
                    $input
                );

            if ($adresoult==true){
                return redirect('Admin/links');
            }else{
                return back()->with('errors','添加失败');
            }

        }else{
            return back()->withErrors($validator);
        }
    }

  //修改
    //GET|HEAD.Admin/links/{index}/edit
    function edit ($link_id){
        $upresoult=DB::table('links')
            ->where('link_id',$link_id)
            ->get();

        return view('Admin/links/update',compact('upresoult'));
    }
    //PUT|PATCH.Admin/links/{index
    function update($link_id){
        $up=Input::except('_token','_method');
        $upcount=  DB::table('links')
            ->where('link_id',$link_id)
            ->update(
                $up
            );

        if ($upcount>0){
            return redirect('Admin/links');
        }else{
            return back()->withErrors('errors',修改失败);
        }

    }
//删除
    //DELETE Admin/index/{index}
    function destroy($link_id){

        $delcount=DB::table('links')
            ->where('link_id',$link_id)
            ->delete();
        if ($delcount<=0){
            $update=[
                'status'=>0,
                'msg'=>'删除失败,请稍后再试',
            ];
        }else{
            $update=[
                'status'=>1,
                'msg'=>'删除成功',
            ];

        }
        return $update;
    }




}
